package com.treetao.union.web.controller.mall;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.CommonUser;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.CommonUserService;
import com.treetao.union.web.constant.Constants;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * <p>
 * 前端用户  前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/common-user")
public class CommonUserController {

    @Autowired
    private CommonUserService commonUserService;

    @GetMapping("list")
    public String list(){
        return "/admin/common-user/list";
    }

    @PostMapping("list")
    @ResponseBody
    public LayerData<CommonUser> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<CommonUser> layerData = new LayerData<>();
        QueryWrapper<CommonUser> wrapper = new QueryWrapper<CommonUser>();
        if(!map.isEmpty()){
        	String delFlag = (String) map.get("delFlag");
            if(StringUtils.isNotBlank(delFlag)) {
                wrapper.eq("del_flag",Boolean.valueOf(delFlag));
            }else{
                map.remove("del_flag");
            }
            String username = (String) map.get("username");
            if(StringUtils.isNotBlank(username)) {
                wrapper.like("username",username);
            }else{
                map.remove("username");
            }

            String phone = (String) map.get("phone");
            if(StringUtils.isNotBlank(phone)) {
                wrapper.like("phone",phone);
            }else{
                map.remove("phone");
            }

            String beginRegtime = (String) map.get("beginRegtime");
            String endRegtime = (String) map.get("endRegtime");
            if(StringUtils.isNotBlank(beginRegtime)) {
                Date begin = DateUtil.parse(beginRegtime);
                wrapper.ge("regtime",begin);
            }else{
                map.remove("beginRegtime");
            }
            if(StringUtils.isNotBlank(endRegtime)) {
                Date end = DateUtil.parse(endRegtime);
                wrapper.le("regtime",end);
            }else{
                map.remove("endRegtime");
            }

        }
        IPage<CommonUser> pageData = commonUserService.page(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    public String add(){
        return "/admin/common-user/add";
    }

    @PostMapping("add")
    @ResponseBody
    public RestResponse add(CommonUser commonUser){
        commonUserService.save(commonUser);
        return RestResponse.success();
    }
    
    @GetMapping("detail")
    public String detail(Long id,Model model){
       CommonUser commonUser = commonUserService.getById(id);
        model.addAttribute("commonUser",commonUser);
        return "/admin/common-user/detail";
    }

    @GetMapping("edit")
    public String edit(Long id,Model model){
        CommonUser commonUser = commonUserService.getById(id);
        model.addAttribute("commonUser",commonUser);
        return "/admin/common-user/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public RestResponse edit(CommonUser commonUser){
        if(null == commonUser.getId() || 0 == commonUser.getId()){
            return RestResponse.failure("ID不能为空");
        }
        commonUserService.updateById(commonUser);
        return RestResponse.success();
    }
    
    @PostMapping("batchDel")
    @ResponseBody
    public RestResponse batchDel(@RequestParam(value = "ids[]",required = false)List<Long> ids){
        if(null == ids || ids.size()==0){
            return RestResponse.failure("请选择一项");
        }
        return RestResponse.success().setDataOnlyResult( commonUserService.removeByIds(ids));
    }

    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        return RestResponse.success().setDataOnlyResult(commonUserService.removeById(id));
    }
    
    @PostMapping("change-status")
    @ResponseBody
    public RestResponse change_status(@RequestParam(value = "id",required = true)Long id,@RequestParam(value = "status",required = false)Boolean status){
       CommonUser commonUser = commonUserService.getById(id);
        commonUser.setDelFlag(status);
        commonUserService.updateById(commonUser);
        return RestResponse.success();
    }

}